package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.AdminDao;
import com.hs.panghu.dao.imp.AdminDaoImpl;
import com.hs.panghu.entity.Admin;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.设置编码方式
//        2.请求验证信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        System.out.println(username);
//        System.out.println(password);
//        3.数据验证
//        4.连接数据库
        AdminDao adminDao = new AdminDaoImpl();
        Admin admin = adminDao.selectAdminByUsernameAndPassword(username, password);
//        5.给出响应
        if(admin == null){ //用户名或密码错误
            Result error = Result.error();
            String srrstr = JSON.toJSONString(error);
            resp.getWriter().write(srrstr);
        }else{
            Result success = Result.success();
            resp.getWriter().write(JSON.toJSONString(success));
        }
    }
}
